package com.finalBlog.common;

import org.apache.log4j.Logger;

import com.finalBlog.Plugin.RoutePlugin;
import com.finalBlog.interceptor.ParamInterceptor;
import com.finalBlog.model._MappingKit;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.cache.EhCache;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

public class SysConfig extends JFinalConfig{
	private Logger log = Logger.getLogger(SysConfig.class);
	public static Prop prop = PropKit.use("baseConfig.properties"); 
	@Override
	public void configConstant(Constants me) {
		log.info("正在加载一些常量信息配置.....");
		//开发模式
		me.setDevMode(prop.getBoolean("config.devmode", false));
		//设置字符集
		me.setEncoding(prop.get("config.encoding"));
		//设置视图  JFinal 支持 JSP、FreeMarker、Velocity 三种常用视图
		me.setViewType(ViewType.FREE_MARKER);
		me.setError404View("/view/common/404.html");
	}

	@Override
	public void configRoute(Routes me) {
		//绑定路由信息
		new RoutePlugin(me).start();
	}

	@Override
	public void configPlugin(Plugins me) {
		log.info("正在配置Druid数据库连接池属性信息......");
		DruidPlugin druidPlugin = new DruidPlugin(
				prop.get("db.jdbcUrl"), 
				prop.get("db.userName"),
				prop.get("db.passWord"),
				prop.get("db.driver"));
		druidPlugin.set(
				prop.getInt("db.initialSize"), 
				prop.getInt("db.minIdle"), 
				prop.getInt("db.maxActive"));
		me.add(druidPlugin);
		
		log.info("正在配置ActiveRecordPlugin 数据源插件 ");
		ActiveRecordPlugin recordPlugin = new ActiveRecordPlugin(prop.get("db.configname"), druidPlugin);
		me.add(recordPlugin);
		recordPlugin.setDevMode(prop.getBoolean("config.devmode"));
		recordPlugin.setShowSql(prop.getBoolean("db.showsql"));
		recordPlugin.setDialect(new MysqlDialect());
		recordPlugin.setContainerFactory(new CaseInsensitiveContainerFactory(true));// 配置属性名(字段名)大小写不敏感容器工厂
		recordPlugin.setCache(new EhCache());
		log.info("正在为ActiveRecordPlugin 添加表的映射");
		_MappingKit.mapping(recordPlugin);
		//断电、应用服务器down 掉后重启使用旧缓存
		System.setProperty("net.sf.ehcache.enableShutdownHook","true");
		me.add(new EhCachePlugin());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new ParamInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new DruidStatViewHandler("/mydruid"));
	}

}
